import axios from "axios";
import { ElNotification } from "element-plus";
import { h } from "vue";

const http = axios.create({
  baseURL: "https://classtool.liuyahui632.com/",
  timeout: 30000,
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Content-Type": "application/json;charset=UTF-8",
  },
});

// axios.defaults.baseURL = "https://young.flgqt.com/";
// axios.defaults.timeout = 300000;
// axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
// axios.defaults.headers.post["Access-Control-Allow-Origin"] = "*";

http.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    console.error(error);
    ElNotification({
      title: "Error",
      message: h("i", { style: "color: teal" }, "请求失败"),
      type: "error",
    });
    return Promise.reject(error);
  }
);

http.interceptors.response.use(
  (response) => {
    // 解析并处理响应数据
    const res = response.data;

    // 如果是二进制流则直接返回
    if (ArrayBuffer.isView(res)) {
      return response;
    }

    ElNotification({
      title: "Warning",
      message: h("i", { style: "color: teal" }, "This is a reminder"),
      type: "warning",
    });

    return response;
  },
  (error) => {
    ElNotification({
      title: "Error",
      message: h("i", { style: "color: teal" }, "网络错误"),
      type: "error",
    });
    return Promise.reject(error);
  }
);

export default http;
